
// INCLUDES ********************************************************************

#include "Texture2D.h"

#include "AnimatedSprite.h"

namespace
{
    const float gDefaultAnimationTime = 0.5f;
}

// NAMESPACES ******************************************************************


#pragma mark -
#pragma mark Constructors/Destructor
// -----------------------------------------------------------------------------

Meteor::AnimatedSprite::AnimatedSprite( const std::string& fileName,
                                        const int numFrames,
                                        const Meteor::Vector2D& frameSize)

: Sprite( fileName), mAnimationTime( gDefaultAnimationTime), mNumFrames( numFrames), 
  mFrameSize( frameSize), mFrameRect( 0, 0, frameSize.X(), frameSize.Y())
{
    Sprite::SetTextureRect( mFrameRect);
}

// -----------------------------------------------------------------------------

Meteor::AnimatedSprite::AnimatedSprite( const Meteor::AnimatedSprite& sprite)

:Sprite( sprite)
{
    mAnimationTime = sprite.mAnimationTime;
    
    mCurrentAnimation = sprite.mCurrentAnimation;
    
    mNumFrames = sprite.mNumFrames;
    
    mFrameRect = sprite.mFrameRect;
}

// -----------------------------------------------------------------------------

Meteor::AnimatedSprite::~AnimatedSprite()
{
}

#pragma mark -
#pragma mark Animation time
// -----------------------------------------------------------------------------

void Meteor::AnimatedSprite::SetAnimationTime( const float time)
{
    mAnimationTime = time;
    
    if ( mAnimationTime < 0.0f)
    {
        mAnimationTime = 0;
    }
}

#pragma mark -
#pragma mark Animations
// -----------------------------------------------------------------------------

void Meteor::AnimatedSprite::SetCurrentAnimation( const uint32_t animation)
{
    const int maxAnimations = mTexture->Height()/mFrameSize.Y();

    if ( animation > maxAnimations)
    {
        Logger::DebugLog( "AnimatedSprite::SetCurrentAnimation - Invalid animation");
    
        return;
    }
    
    mCurrentAnimation = animation;
    
    mFrameRect.SetY( mFrameSize.Y() * (animation-1));
}

#pragma mark -
#pragma mark Frames
// -----------------------------------------------------------------------------

void Meteor::AnimatedSprite::SetNumFrames( const uint32_t numFrames)
{
    const int maxFrames = static_cast<int>(mTexture->Width()/mFrameSize.X());

    if ( numFrames > maxFrames)
    {
        Logger::DebugLog( "AnimatedSprite::SetNumFrames - Invalid number of frames");
    
        return;
    }
    
    mNumFrames = numFrames;
}


#pragma mark -
#pragma mark Update
// -----------------------------------------------------------------------------

void Meteor::AnimatedSprite::Update( const float deltaTime)
{
    const float timePerFrame = mAnimationTime / mNumFrames;
    
    static float accuTime = 0;
    
    static int frameCounter = 0;
    
    if ( accuTime > timePerFrame)
    {
        mFrameRect.SetX(( frameCounter % mNumFrames ) * mFrameSize.X());
        
        mTexture->SetRect( mFrameRect);
        
        accuTime = 0;
        
        frameCounter++;
    }
    
    accuTime += deltaTime;
}
